import path from 'path';
import webpack from 'webpack';
import FriendlyErrorsPlugin from 'friendly-errors-webpack-plugin';
import ProgressBarPlugin from 'progress-bar-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import OptimizeCSSPlugin from 'optimize-css-assets-webpack-plugin';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import htmlMinifyOptions from '../config/htmlminify';
const Dir = global.DIR;

export const getPlugins = (env, TYPE) => {
	const GLOBALS = {
		'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
		'global.CONFIG': JSON.stringify(global.CONFIG),
		'global.TYPE': JSON.stringify(TYPE),
	};
	const plugins = [
		new webpack.DefinePlugin(GLOBALS)
	];
	if (TYPE === 'SERVER') {
		return plugins;
	};
	// plugins.push(new CleanWebpackPlugin(path.resolve(__dirname, '../dist'), {
	//     root: path.resolve(__dirname, '../'),    // 设置root
	//     verbose: true
	// }));
	plugins.push(new webpack.optimize.OccurrenceOrderPlugin());
	plugins.push(new webpack.optimize.CommonsChunkPlugin({
		name: 'vendor',
		minChunks: (module, count) => {
			// any required modules inside node_modules are extracted to vendor
			return (
				module.resource &&
				/\.js$/.test(module.resource) &&
				module.resource.indexOf(
					path.join(__dirname, '../node_modules')
				) === 0
			);
		}
	}));
	plugins.push(new webpack.optimize.CommonsChunkPlugin({
		name: 'manifest',
		chunks: ['vendor']
	}));
	// 分离 CSS 代码
	plugins.push(new ExtractTextPlugin({
		filename: '[name].[hash].css',
		ignoreOrder: true
	}));
	switch (env) {
		case 'production':
			// 压缩提取出的 CSS，并解决ExtractTextPlugin分离出的 JS 重复问题
			plugins.push(new OptimizeCSSPlugin({
				cssProcessorOptions: {
					safe: true
				}
			}));
			plugins.push(new CleanWebpackPlugin(['dist', 'build'], {
				root: Dir.root,
				verbose: true,
				dry: false,
				//exclude: ["dist/1.chunk.js"]
			}));
			// 压缩 JS 代码
			plugins.push(new webpack.optimize.UglifyJsPlugin({
				// 最紧凑的输出
				// beautify: true,
				// minify:true,
				// exclude: /\.min\.js$/,
				mangle: true,
				output: { comments: false },
				compress: {
					// 在UglifyJs删除没有用到的代码时不输出警告
					warnings: false,
					// 内嵌定义了但是只用到一次的变量
					collapse_vars: true,
					// 提取出出现多次但是没有定义成变量去引用的静态值
					reduce_vars: true,
					// 删除所有的 `console` 语句
					// 还可以兼容ie浏览器
					drop_console: true,
					drop_debugger: true
				}
			}));
			plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
			break;
		case 'development':

			plugins.push(new webpack.NamedModulesPlugin());
			plugins.push(new HtmlWebpackPlugin({
				title: 'gavin home',
				filename: path.join('..', '..', Dir._dist, 'web.html'), //生成的html存放路径，相对于 path
				template: path.join(Dir.views, 'index.tpl.html'),
				inject: true,
				minify: htmlMinifyOptions,
				favicon: path.join(Dir.views, 'favicon.ico')
			}));
			plugins.push(new webpack.HotModuleReplacementPlugin());
			plugins.push(new webpack.NoEmitOnErrorsPlugin());
			// plugins.push(new ProgressBarPlugin({
			// 	summary: true
			// }));
			break;
	};
	// plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
	plugins.push(new FriendlyErrorsPlugin());
	return plugins;
};
